package com.aqie.arithmetic.datastructure;

import com.aqie.libs.StdOut;

import java.util.Arrays;

/**
 * 使用泛型优化 固定容量的数组
 * - problem:
 * @param <T>
 */
public class FixedCapacityStack<T> {
    private T[] a;
    private int N;

    public FixedCapacityStack(int cap) {
        a = (T[])new Object[cap];
    }

    public boolean isEmpty(){
        return N == 0;
    }
    public int size(){
        return N;
    }

    public void push(T item){
        a[N++] = item;
    }

    public T pop(){
        return a[--N];
    }

    public static void main(String[] args) {
        FixedCapacityStack<String> stack = new FixedCapacityStack<>(100);
        String str = "-   to be or not - to - be - - that - - - is -";
        String[] arr = str.split("\\s");
        System.out.println(Arrays.toString(arr));
        for (String x : arr){
            if (!x.equals("-")){
                stack.push(x);
            }else if (!stack.isEmpty()){
                StdOut.print(stack.pop() + " ");
            }
        }
        StdOut.println("(" + stack.size() + " left on stack");
    }
}
